package com.zhangpingyang.jwttoken.service.impl;

import com.zhangpingyang.jwttoken.dao.UmsAdminRoleRelationDao;
import com.zhangpingyang.jwttoken.mgb.mapper.UmsAdminMapper;
import com.zhangpingyang.jwttoken.mgb.model.UmsAdmin;
import com.zhangpingyang.jwttoken.mgb.model.UmsAdminExample;
import com.zhangpingyang.jwttoken.mgb.model.UmsPermission;
import com.zhangpingyang.jwttoken.service.UmsAdminService;
import com.zhangpingyang.jwttoken.util.JwtTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class UmsAdminServiceImpl implements UmsAdminService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
    @Autowired
    private UmsAdminMapper adminMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    //    @Autowired
//    private UmsAdminRoleRelationMapper adminRoleRelationMapper;
    @Autowired
    private UmsAdminRoleRelationDao umsAdminRoleRelationDao;

    @Override
    public UmsAdmin getAdminByUsername(String username) {
        List<UmsAdmin> umsAdmins = getUmsAdmins(username);
        return umsAdmins == null || umsAdmins.size() == 0 ? null : umsAdmins.get(0);
    }

    private List<UmsAdmin> getUmsAdmins(String username) {
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        return adminMapper.selectByExample(example);
    }

    @Override
    public UmsAdmin register(UmsAdmin umsAdminParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
//        List<UmsAdmin> umsAdminList = getUmsAdmins(umsAdmin.getUsername());
//        if (umsAdminList.size() > 0) {
//            return null;
//        }
        UmsAdminExample example = new UmsAdminExample();
        UmsAdminExample.Criteria criteria = example.createCriteria().andUsernameEqualTo(umsAdmin.getUsername());
        int i = adminMapper.countByExample(example);
        if (i > 0) {
            return null;
        }
        umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword()));
        adminMapper.insert(umsAdmin);
        return umsAdmin;
    }

    @Override
    public String login(String username, String password) {
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (!passwordEncoder.matches(password, userDetails.getPassword())){
            throw new BadCredentialsException("密码错误");
        }
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        return jwtTokenUtil.generatorToken(userDetails);
    }

    @Override
    public List<UmsPermission> getPermissionList(Long adminId) {
        return umsAdminRoleRelationDao.getPermissionList(adminId);
//        return Collections.emptyList();
    }
}
